import axios from '@/utils/request'

import type { IListResult, IPageResult } from './types/base'
import { IClaimType } from './types/claimTypes'
import { IRole } from './types/role'
import type { IUser, IUserClaim, IUserData } from './types/user'

// 获取用户列表
export function getUsers(
  currentPage = 1,
  take = 10,
  params: {
    filter: string
  },
) {
  return axios<IPageResult<IUser>>({
    url: '/api/identity/users',
    method: 'get',
    params: {
      skipCount: take * (currentPage - 1),
      maxResultCount: take,
      ...params,
    },
  })
}

// 查询用户详情
export function getUser(id: string) {
  return axios<IUser>({
    url: `/api/identity/users/${id}`,
    method: 'get',
  })
}
// 查询可以使用的角色
export function assignableRoles() {
  return axios<IListResult<IRole>>({
    url: '/api/identity/users/assignable-roles',
    method: 'get',
  })
}

// 新增用户
export function addUser(data: IUserData) {
  return axios<IUser>({
    url: '/api/identity/users',
    method: 'post',
    data,
  })
}

// 修改角色
export function updateUser(id: string, data: IUserData) {
  return axios<IUser>({
    url: `/api/identity/users/${id}`,
    method: 'put',
    data,
  })
}

// 查询用户权限
export function getUserPermission(id: string) {
  return axios<IListResult<IRole>>({
    url: `/api/identity/users/${id}/roles`,
    method: 'get',
  })
}

// 删除用户
export function deleteUser(id: string) {
  return axios({
    url: `/api/identity/users/${id}`,
    method: 'delete',
  })
}

// 获取所有声明
export function getAllUserClaimTypes() {
  return axios<IClaimType[]>({
    url: '/api/identity/user/all-claim-types',
    method: 'get',
  })
}
// 获取当前用户的声明
export function getUserClaims(userId: string) {
  return axios<IUserClaim[]>({
    url: `/api/identity/user/${userId}/claims`,
    method: 'get',
  })
}

// 保存当前用户的声明
export function setUserClaims(userId: string, data: IUserClaim[]) {
  return axios({
    url: `/api/identity/user/${userId}/claims`,
    method: 'put',
    data,
  })
}
